package org.whuims.core.dataprocessor;

import org.apache.commons.io.FileUtils;
import org.springframework.web.util.HtmlUtils;
import org.whuims.core.utils.Mysql;

import java.io.File;
import java.sql.*;
import java.util.*;

/**
 * Created by cheng on 2016/11/1.
 */
public class DataProcessor {
    String dirPath = "";

    public DataProcessor(String dirPath) {
        this.dirPath = dirPath;
    }

    public static void main(String[] args) {
        String dirPath = "D:\\work\\ideaproject\\stdmem\\src\\main\\webapp\\resource";
        DataProcessor processor = new DataProcessor(dirPath);
        processor.process();
    }

    /**
     * 遍历每个文件，读入并处理。
     */
    public void process() {
        List<File> txtFiles = new ArrayList<File>();
        List<File> pdfFiles = new ArrayList<File>();
        crawlDir(txtFiles, new File(dirPath + "\\txt"));
        crawlDir(pdfFiles, new File(dirPath + "\\pdf"));

        System.out.println("txt数量：" + txtFiles.size());
        System.out.println("pdf数量：" + pdfFiles.size());
        //addEntries(txtFiles);
        //cleanDate();
        addText(txtFiles);
    }

    public void addText(List<File> txtFiles) {
        Connection conn = Mysql.getConn("stdmem");
        try {
            Map<String, String> map = new HashMap<String, String>();
            for (File file : txtFiles) {
                String text = FileUtils.readFileToString(file, "utf8");
                String fileName = file.getName();
                //GB/T 21362-2008
                String name = fileName.substring(0, fileName.lastIndexOf('.'));
                name = name.replace("-T", "/T");
                name = name.replace("-Z", "/Z");
                name = name.replace("HJ_", "HJ_T");
                name = name.replace("HJ_TT", "HJ_T");
                if (name.matches(".+?_\\d.+?")) {
                    name = name.substring(0, name.lastIndexOf('_'));
                }
                name = name.replace("_T", "/T");
                name = name.replace("HJ/T", "HJ/T ");
                name = name.replace("  ", " ");
                text = new String(text.getBytes(), "UTF-8");
                text = HtmlUtils.htmlEscape(text);
                map.put(name, text);
            }
            Set set = new HashSet<String>();
            set.add("GB 50055-2011");
            set.add("GB/T 50893-2013");
            set.add("GB 50506-2009");
            set.add("GB/T 27840-2011");
            for (Map.Entry<String, String> entry : map.entrySet()) {
//                if(!entry.getKey().equals("GB/T 50893-2013")){
//                    continue;
//                }
                PreparedStatement pstmt = conn.prepareStatement("update std_std set text_ocr =? where name=?");
                System.out.println(entry.getValue().length());
                pstmt.setString(1, entry.getValue());
                pstmt.setString(2, entry.getKey());
                pstmt.execute();
                pstmt.close();
                System.out.println("==================");
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cleanDate() {
        Map<Long, String> map = new HashMap<Long, String>();
        Connection conn = Mysql.getConn("stdmem");
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from std_std");
            while (rs.next()) {
                Long id = rs.getLong("id");
                String publishDate = rs.getString("effective_date");
                if (publishDate == null || publishDate.equals("")) continue;
                if (!publishDate.contains("-")) {
                    System.out.println(publishDate);
                    String year = publishDate.substring(0, 4);
                    String month = publishDate.substring(4, 6);
                    String day = publishDate.substring(6, 8);
                    String date = year + "-" + month + "-" + day;
                    map.put(id, date);
                }
            }
            rs.close();
            for (Map.Entry<Long, String> entry : map.entrySet()) {
                String sqlString = "update std_std set effective_date='" + entry.getValue() + "' where id=" + entry.getKey() + ";";
                stmt.execute(sqlString);
            }
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将pdf文件对应的记录加入到数据库中
     *
     * @param files
     */
    public void addEntries(List<File> files) {
        Connection conn = Mysql.getConn("stdmem");
        try {
            Statement stmt = conn.createStatement();
            for (File file : files) {
                String dirName = file.getParentFile().getName();
                String fileName = file.getName();
                //GB/T 21362-2008
                String name = fileName.substring(0, fileName.lastIndexOf('.'));
                name = name.replace("-T", "/T");
                name = name.replace("-Z", "/Z");
                name = name.replace("HJ_", "HJ_T");
                name = name.replace("HJ_TT", "HJ_T");
                if (name.matches(".+?_\\d.+?")) {
                    name = name.substring(0, name.lastIndexOf('_'));
                }
                name = name.replace("_T", "/T");
                name = name.replace("HJ/T", "HJ/T ");
                name = name.replace("  ", " ");
                System.out.println(fileName + "\t" + name);
                String sql = "UPDATE std_std SET cat_std='" + dirName + "' where name='" + name + "'";
                System.out.println(sql);
                stmt.execute(sql);
            }
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void crawlDir(List<File> files, File file) {
        if (file.isFile()) {
            files.add(file);
        } else if (file.isDirectory()) {
            for (File subFile : file.listFiles()) {
                crawlDir(files, subFile);
            }
        }
    }
}
